﻿using System;
using System.Globalization;
using System.Windows.Forms;
using RaGE.Properties;

namespace RaGE
{
    public partial class ReplaceWindow : Form
    {
        public ReplaceWindow(EListCollection listCollection)
        {
            ELC = listCollection;
            InitializeComponent();
            comboBox_operation.SelectedIndex = 0;
        }

        private void ButtonReplaceClick(object sender, EventArgs ea)
        {
            Cursor = Cursors.AppStarting;

            var replacementCount = 0;
            int l;
            int lMax;
            var oldValue = textBox_oldValue.Text;
            var newValue = textBox_newValue.Text;
            var operation = comboBox_operation.SelectedItem.ToString();
            var operand = numericUpDown_operand.Value;

            if (textBox_list.Text == Resources.ASTERISK)
            {
                l = 0;
                lMax = ELC.Lists.Length;
            }
            else
            {
                l = Convert.ToInt32(textBox_list.Text);
                lMax = Convert.ToInt32(textBox_list.Text) + 1;

                if (l < 0 || lMax > ELC.Lists.Length)
                {
                    MessageBox.Show(Resources.ListIdxOORng);
                    return;
                }
            }

            for ( ; l < lMax; l++)
            {
                int e;
                int eMax;

                if (textBox_item.Text == Resources.ASTERISK)
                {
                    e = 0;
                    eMax = ELC.Lists[l].elementValues.Length;
                }
                else
                {
                    e = Convert.ToInt32(textBox_item.Text);
                    eMax = Convert.ToInt32(textBox_item.Text) + 1;

                    if (e < 0 || eMax > ELC.Lists[l].elementValues.Length)
                    {
                        MessageBox.Show(Resources.ItemIdxOORng);
                        return;
                    }
                }

                for ( ; e < eMax; e++)
                {
                    int v;
                    int vMax;

                    if (textBox_field.Text == Resources.ASTERISK)
                    {
                        v = 0;
                        vMax = ELC.Lists[l].elementValues[e].Length;
                    }
                    else
                    {
                        v = Convert.ToInt32(textBox_field.Text);
                        vMax = Convert.ToInt32(textBox_field.Text) + 1;

                        if (v < 0 || vMax > ELC.Lists[l].elementValues[e].Length)
                        {
                            MessageBox.Show(Resources.FieldIdxOORng);
                            return;
                        }
                    }

                    for ( ; v < vMax; v++)
                    {
                        if (radioButton_replace.Checked)
                        {
                            if (oldValue == "*" || oldValue == ELC.GetValue(l, e, v).Trim(new[] { (char) 0 }))
                            {
                                ELC.SetValue(l, e, v, newValue);
                                replacementCount++;
                            }
                        }

                        if (!radioButton_recalculate.Checked) continue;

                        var type = ELC.GetType(l, v);

                        switch (type)
                        {
                            case "int16":
                                {
                                    var op1 = Convert.ToInt16(ELC.GetValue(l, e, v));
                                    var op2 = Convert.ToInt16(operand);
                                    var result = "";

                                    switch (operation)
                                    {
                                        case "*":
                                            result = (op1 * op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "/":
                                            result = (op1 / op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "+":
                                            result = (op1 + op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "-":
                                            result = (op1 - op2).ToString(CultureInfo.InvariantCulture);
                                            break;
                                    }

                                    ELC.SetValue(l, e, v, result);
                                    replacementCount++;
                                }
                                break;

                            case "int32":
                                {
                                    var op1 = Convert.ToInt32(ELC.GetValue(l, e, v));
                                    var op2 = Convert.ToInt32(operand);
                                    var result = "";

                                    switch (operation)
                                    {
                                        case "*":
                                            result = (op1 * op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "/":
                                            result = (op1 / op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "+":
                                            result = (op1 + op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "-":
                                            result = (op1 - op2).ToString(CultureInfo.InvariantCulture);
                                            break;
                                    }

                                    ELC.SetValue(l, e, v, result);
                                    replacementCount++;
                                }
                                break;

                            case "int64":
                                {
                                    var op1 = Convert.ToInt64(ELC.GetValue(l, e, v));
                                    var op2 = Convert.ToInt64(operand);
                                    var result = "";

                                    switch (operation)
                                    {
                                        case "*":
                                            result = (op1 * op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "/":
                                            result = (op1 / op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "+":
                                            result = (op1 + op2).ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "-":
                                            result = (op1 - op2).ToString(CultureInfo.InvariantCulture);
                                            break;
                                    }

                                    ELC.SetValue(l, e, v, result);
                                    replacementCount++;
                                }
                                break;

                            case "float":
                                {
                                    var op1 = Convert.ToSingle(ELC.GetValue(l, e, v));
                                    var op2 = Convert.ToSingle(operand);
                                    var result = "";

                                    switch (operation)
                                    {
                                        case "*":
                                            result = (op1 * op2).ToString("F3");
                                            break;

                                        case "/":
                                            result = (op1 / op2).ToString("F3");
                                            break;

                                        case "+":
                                            result = (op1 + op2).ToString("F3");
                                            break;

                                        case "-":
                                            result = (op1 - op2).ToString("F3");
                                            break;
                                    }

                                    ELC.SetValue(l, e, v, result);
                                    replacementCount++;
                                }
                                break;

                            case "double":
                                {
                                    var op1 = Convert.ToDouble(ELC.GetValue(l, e, v));
                                    var op2 = Convert.ToDouble(operand);
                                    var result = "";

                                    switch (operation)
                                    {
                                        case "*":
                                            result = (op1 * op2).ToString("F3");
                                            break;

                                        case "/":
                                            result = (op1 / op2).ToString("F3");
                                            break;

                                        case "+":
                                            result = (op1 + op2).ToString("F3");
                                            break;

                                        case "-":
                                            result = (op1 - op2).ToString("F3");
                                            break;
                                    }

                                    ELC.SetValue(l, e, v, result);
                                    replacementCount++;
                                }
                                break;
                        }
                    }
                }
            }

            Cursor = Cursors.Default;

            MessageBox.Show(replacementCount + Resources.ValuesReplaced);
        }

        private void ButtonCancelClick(object sender, EventArgs e)
        {
            Close();
        }



    }
}
